Selectable List
List.selection provides selection state binding for the List component. It enables:
- Single selection mode
- Multiple selection mode
- Integration with edit mode via
EditButton - Automatic synchronization with user interaction
1. API Definition
2. selection Type Description
The selection mode is determined by the generic type of Observable:
3. Automatic Binding Rules with ForEach
When List is bound to selection, every item inside ForEach.data must conform to the following structure:
Binding behavior:
-
The
idproperty is automatically used as the unique selection identifier -
When a list item is tapped:
- Single selection mode:
selected.valueis automatically set to the tapped item’sid - Multiple selection mode: the
idis automatically added to or removed fromselected.value
- Single selection mode:
-
Manual tap handling is not required
-
The
idvalue must remain unique and stable; otherwise, selection behavior becomes undefined
4. Single Selection Mode
1. Definition
2. Usage Example
3. State Description
null: no item is currently selected"3": the item whoseidis"3"is selected
5. Multiple Selection Mode
1. Definition
2. Usage Example
3. State Description
selected.value is always an array of strings, for example:
This indicates that three items are currently selected.
6. Interaction Between selection and EditButton
When List is bound to selection:
-
EditButtonautomatically enables list editing mode -
While in edit mode:
- Single selection: tapping an item replaces the current selection
- Multiple selection: multiple items can be selected simultaneously
-
After exiting edit mode:
-
selected.valueis automatically reset- Single selection resets to
null - Multiple selection resets to an empty array
[]
- Single selection resets to
-
This behavior matches SwiftUI’s native edit mode behavior.
7. Programmatic Control of selection
In addition to user interaction, selection can be modified by code.
Single Selection
Multiple Selection
The UI will update automatically to reflect the new selection state.
8. Compatibility with NavigationStack
List.selection is fully compatible with NavigationStack and does not affect:
- Navigation behavior
- Toolbar layout
- Edit mode interactions
- Back navigation behavior
Recommended structure:
9. Common Errors and Misuse
1. Incorrect selection Type
Incorrect:
Correct:
Currently, only string is supported as the selection identifier type.
2. Incorrect Initialization for Multiple Selection
Incorrect:
Correct:
3. Missing id in ForEach.data
Incorrect:
This will cause:
- Selection to fail
- Unstable checked state
- List reuse inconsistencies
10. Typical Use Cases
List.selection is suitable for:
- Single-choice settings (themes, languages, preferences)
- Batch deletion
- Batch export
- Batch sharing
- File managers
- Contact pickers
- Task lists with selection
